$(function () {
    var startx, starty;
    let index = 0;

//获得角度
    function getAngle(angx, angy) {
        return Math.atan2(angy, angx) * 180 / Math.PI;
    };

//根据起点终点返回方向 1向上 2向下 3向左 4向右 0未滑动
    function getDirection(startx, starty, endx, endy) {
        var angx = endx - startx;
        var angy = endy - starty;
        var result = 0;
        //如果滑动距离太短
        if (Math.abs(angx) < 2 && Math.abs(angy) < 2) {
            return result;
        }
        var angle = getAngle(angx, angy);
        if (angle >= -135 && angle <= -45) {
            result = 1;
        } else if (angle > 45 && angle < 135) {
            result = 2;
        } else if ((angle >= 135 && angle <= 180) || (angle >= -180 && angle < -135)) {
            result = 3;
        } else if (angle >= -45 && angle <= 45) {
            result = 4;
        }
        return result;
    }

//手指接触屏幕
    document.getElementById('navigation-select-item').addEventListener("touchstart", function (e) {
        startx = e.touches[0].pageX;
        starty = e.touches[0].pageY;
    }, false);
//手指离开屏幕
    document.getElementById('navigation-select-item').addEventListener("touchend", function (e) {
        var endx, endy;
        endx = e.changedTouches[0].pageX;
        endy = e.changedTouches[0].pageY;
        var direction = getDirection(startx, starty, endx, endy);

        switch (direction) {
            case 0:
                // console.log("未滑动！");
                break;
            case 1:
                // console.log("向上！");
                break;
            case 2:
                // console.log("向下！");
                break;
            case 3:
                // console.log("向左！");
                slideMain.slideLeft();
                break;
            case 4:
                // console.log("向右！");
                slideMain.slideRight();
                break;
            default:
        }
    }, false);
    // 滑动部分逻辑
    const slideMain = {
        // 向左滑动
        slideLeft: function () {
            if (index < 2) {
                index++;
                slideMain.addRemoveClass(index);
                $('#navigation-select-item').animate({left: '+=-9.6rem'});
            } else if (index === 2 || index > 2) {
                index = 2;
                let nav = $('#navigation-select-item');
                nav.animate({left: '-9.86667' * index + 'rem'}, "slow");
                nav.animate({left: '-9.6' * index + 'rem'}, 'slow');
            }
        },
        // 向右滑动
        slideRight: function () {
            if (index < 3 && index > 0) {
                index--;
                slideMain.addRemoveClass(index);
                $('#navigation-select-item').animate({left: '+=9.6rem'});
            } else if (index === 0 || index < 0) {
                index = 0;
                let nav = $('#navigation-select-item');
                nav.animate({left: '20px'}, "slow");
                nav.animate({left: '0'}, 'slow');
            }
        },
        // 移除小圆点的class
        addRemoveClass: function (select) {
            for (let i = 0; i < 3; i++) {
                $('#nav-item-location-li' + i).removeClass('nav-item-location-li-active');
            }
            $('#nav-item-location-li' + select).addClass('nav-item-location-li-active');
        },
    };

});